package com.kingyun.gpsinspection.purificationservice.services.tdm.outerservice.impl;

import com.kingyun.gpsinspection.purificationservice.dao.mapper.tdm.*;
import com.kingyun.gpsinspection.purificationservice.facade.model.tdm.*;
import com.kingyun.gpsinspection.purificationservice.facade.service.activiti.base.WorkFlowService;
import com.kingyun.gpsinspection.purificationservice.facade.service.tdm.FinishTaskWebService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * Created by sugongche on 2017/3/14.
 */
public class FinishTaskWebServiceImpl implements FinishTaskWebService {
    public Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private TdmWorkOrderMapper tdmWorkOrderMapper;
    @Autowired
    private TaskMapper taskMapper;
    @Autowired
    private PatrolInspectionMapper patrolInspectionMapper;
    @Autowired
    private NormalOperationMapper normalOperationMapper;
    @Autowired
    private MaintenanceMapper maintenanceMapper;
    @Autowired
    private CheckRepairMapper checkRepairMapper;
    @Autowired
    private TerritorialSupervisionMapper territorialSupervisionMapper;
    @Autowired
    private TdmProblemMapper tdmProblemMapper;
    @Autowired
    private DailyThingsMapper dailyThingsMapper;
    @Autowired
    private WorkFlowService workFlowService;

    /**
     * 所有工单执行完后：完成相应的待办任务
     *
     * @param taskId     任务ID
     * @param employeeId 人员ID
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int finishTaskByTaskId(String taskId, String employeeId) {
        int a = 0;
        try {
            List<WorkOrder> workOrderList = tdmWorkOrderMapper.findListByTaskId(taskId);
            Boolean flag = true;

            if (workOrderList != null && workOrderList.size() > 0) {
                for (WorkOrder workOrder : workOrderList) {
                    //所有工单完成和关闭后表示任务可以结束
                    if (workOrder != null && workOrder.getStatus() != 6 && workOrder.getStatus() != 7) {
                        flag = false;
                    }
                }
            }

            a = 0;

            //工单全部完成：完成任务
            if (flag) {
                Task task = taskMapper.findTaskByTaskId(taskId);
                Short taskType = null == task ? 5 : task.getTaskType();
                if (taskType != null) {
                    //巡回检查
                    if (taskType == 1) {
                        PatrolInspection patrolInspection = new PatrolInspection();
                        patrolInspection.setPatrolInspectionId(task.getId());
                        //更新状态
                        a += patrolInspectionMapper.updateFinishById(patrolInspection);
                        //常规操作
                    } else if (taskType == 2) {
                        NormalOperation normalOperation = new NormalOperation();
                        normalOperation.setNormalOperationId(task.getId());
                        //更新状态
                        a += normalOperationMapper.updateFinishById(normalOperation);
                        //维护保养
                    } else if (taskType == 3) {
                        Maintenance maintenance = new Maintenance();
                        maintenance.setMaintenanceId(task.getId());
                        //更新当前节点
                        a += maintenanceMapper.updateFinishById(maintenance);
                        //检查维修
                    } else if (taskType == 4) {
                        CheckRepair checkRepair = new CheckRepair();
                        checkRepair.setCheckRepairId(task.getId());
                        //更新状态
                        a += checkRepairMapper.updateFinishById(checkRepair);
                        //属地监督
                    } else if (taskType == 5) {
                        Problem problem = new Problem();
                        problem.setProblemId(taskId);
                        problem.setStatus(new Short("2"));
                        problem.setModifyTime(new Date());
                        problem.setModifyUserId(employeeId);
                        //修改问题状态
                        tdmProblemMapper.updateByPrimaryKeySelective(problem);
                        //属地监督
                    } else if (taskType == 7) {
                        TerritorialSupervision territorialSupervision = new TerritorialSupervision();
                        territorialSupervision.setTerritorialSupervisionId(task.getId());
                        //更新状态
                        a += territorialSupervisionMapper.updateFinishById(territorialSupervision);
                    } else if (taskType == 11) {
                        //日常事务
                        DailyThingsVo dailyThingsVo = new DailyThingsVo();
                        dailyThingsVo.setDailythingsId(task.getId());
                        //更新状态
                        a += dailyThingsMapper.updateFinishById(dailyThingsVo);
                    }
                }
                //更新流程
                String id = taskMapper.getTaskIdByBusinessId(taskId);
                workFlowService.completeTask(id, null, null, employeeId);
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }

        return a;
    }
}

